package com.seafwg.multianno.mapper;

import com.seafwg.multianno.domain.User;
import com.seafwg.multianno.domain.Order;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Author seafwg
 * @Date 2022/3/16 9:06 上午
 * @Version 1.0
 * @description xxx
 */

public interface OrderMapper {

    // 通过user id查询order信息
    @Select("select * from multi_order where uid=#{id}")
    List<Order> findOrderByUserId(int id);

    /**
     * 方法一：多表查询，一次性查出来封装数据
     * 查询所有订单:包括订单对应的用户，一个订单对应用户
     * @return
     */
    @Select("select *, o.id oid from multi_order o, multi_user u where o.uid = u.id")
    @Results({
        @Result(property = "id", column="oid"),
        @Result(property = "ordertime", column = "ordertime"),
        @Result(property = "total", column = "total"),
        @Result(property = "user.id", column = "uid"),
        @Result(property = "user.username", column = "username"),
        @Result(property = "user.password", column = "password")
    })
    List<Order> findAll();

    /**
     * 方法二：分步查询，先查询出订单信息，通过@One注解调用用户的查询信息
     * 分析：通过先查询出订单信息，在封装user的结果集中通过uid参数查询user信息
     *      通过one属性的@One注解调用在用户mapper层的方法查询该订单的用户信息
     */
    @Select("select * from multi_order")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "ordertime", column = "ordertime"),
        @Result(property = "total", column = "total"),
        @Result(
            property = "user",      // 封装的属性名称
            column = "uid",         // 根据那个字段查询user表的数据 -> 调用方法的实参
            javaType = User.class,  // 封装的实体类型
            // select属性 表示查询那个接口的方法
            one = @One(select = "com.seafwg.multianno.mapper.UserMapper.findUserById")
        )
    })
    List<Order> findAllOrders();
}
